package com.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author: tgy
 * @Date: 2020-07-27 15:46
 */
public class CountDownLatchTest {


/**
 * 倒计时操作
 * 如 等待两个线程执行完成，在进行接下来的操作
 */
    public static void main(String[] args) throws Exception{


        CountDownLatch countDownLatch = new CountDownLatch(10);

        for (int i = 0; i < 10; i++) {

            new Thread(()->{

                try {

                    TimeUnit.SECONDS.sleep(3);

                }catch (Exception e){

                }
                countDownLatch.countDown();

            }).start();
        }

//        countDownLatch.await();

        if (countDownLatch.await(3,TimeUnit.SECONDS)){

            System.out.println("倒计时结束");
        }else {

            System.out.println("超时...");
        }
    }
}
